package com.jie.other.network

import android.text.TextUtils
import android.widget.Toast
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils
import com.jie.commonlib.common.FileHelper.isDownload
import com.jie.commonlib.network.bean.PageInfo
import com.jie.commonlib.network.bean.ResponseBody
import com.jie.commonlib.network.http.HttpCallBack
import com.jie.commonlib.network.http.HttpCommonConstant
import com.jie.commonlib.network.http.HttpHelper.download
import com.jie.commonlib.network.http.HttpHelper.enqueue
import com.jie.commonlib.support.sputils.UserInfoUtils
import com.jie.commonlib.utils.DesUtil
import com.jie.other.bean.http.*
import com.jie.other.constant.HttpConstant
import com.net.okhttphelper.download.DownloadListener
import com.net.okhttphelper.http.RequestParams
import com.net.okhttphelper.upload.UploadListener
import java.io.File
import java.util.*

/**
 * 执行网络请求
 *
 * @author jie
 */
object HttpTestUtils {
    private var mFileUrl: String? = null
    fun getHttp(mHttpTag: String?) {
        val requestParams = RequestParams.builder(HttpConstant.URL_LOGOUT, mHttpTag)
            .putQuery("appType", "android")
            .build()
        enqueue(requestParams, object : HttpCallBack<ResponseBody<Any?>?> {
            override fun success(response: ResponseBody<Any?>?) {
                LogUtils.i(response.toString())
                showToast(response?.msg)
                UserInfoUtils.getInstance().logout()
            }

            override fun fail(code: Int, msg: String?) {
                LogUtils.i(msg)
                showToast(msg)
            }
        })
    }

    fun postHttp(mHttpTag: String?) {
        val userName = "yunweitest"
        val password = "@@Zdst688"
        val requestParams = RequestParams.builder(HttpConstant.URL_OAUTH_TOKEN, mHttpTag)
            .putQuery("userName", DesUtil.encrypt(userName, HttpCommonConstant.GET_TOKEN_KEY))
            .putQuery("password", DesUtil.encrypt(password, HttpCommonConstant.GET_TOKEN_KEY))
            .putQuery("clientId", "client")
            .putQuery("encryption", "1")
            .post()
            .build()
        enqueue(requestParams, object : HttpCallBack<TokenRes?> {
            override fun success(response: TokenRes?) {
                LogUtils.i("服务器返回：$response")
                showToast(response?.token?.access_token)
                val userInfoUtils = UserInfoUtils.getInstance()
                userInfoUtils.putUserName(userName)
                userInfoUtils.putPassword(password)
                userInfoUtils.putAccessToken(response?.token?.access_token)
            }

            override fun fail(code: Int, msg: String?) {
                LogUtils.e("请求失败：$code$msg")
                showToast(msg)
            }
        })
    }

    fun getList(mHttpTag: String?) {
        val url = HttpConstant.GET_DICT_URL + "industryType"
        val requestParams = RequestParams.builder(url, mHttpTag).build()
        enqueue(requestParams, object : HttpCallBack<ArrayList<DictModel>?> {
            override fun success(response: ArrayList<DictModel>?) {
                LogUtils.i(response.toString())
                showToast(response.toString())
            }

            override fun fail(code: Int, msg: String?) {
                showToast(msg)
            }
        })
    }

    fun getPageInfo(mHttpTag: String?) {
        val requestParams = RequestParams.builder(HttpConstant.GET_UNIT_LIST, mHttpTag)
            .putQuery("pageNum", "1")
            .putQuery("lat", "0")
            .putQuery("lng", "0")
            .build()
        enqueue(
            requestParams,
            object : HttpCallBack<PageInfo<BuildListModel>?> {
                override fun success(response: PageInfo<BuildListModel>?) {
                    LogUtils.i(response.toString())
                    showToast(response.toString())
                }

                override fun fail(code: Int, msg: String?) {
                    showToast(msg)
                }
            })
    }

    fun postPageInfo(mHttpTag: String?) {
        val requestParams = RequestParams.builder(HttpConstant.GET_BUILD_LIST, mHttpTag)
            .putQuery("pageNum", "1")
            .putQuery("lat", "0")
            .putQuery("lng", "0")
            .post()
            .build()
        enqueue(
            requestParams,
            object : HttpCallBack<ResponseBody<PageInfo<BuildListModel>>?> {
                override fun success(response: ResponseBody<PageInfo<BuildListModel>>?) {
                    LogUtils.i(response.toString())
                    showToast(response.toString())
                }

                override fun fail(code: Int, msg: String?) {
                    showToast(msg)
                }
            })
    }

    fun postNewBuilding(mHttpTag: String?) {
//        String requestBody = "{\"buildingAddress\":\"\",\"code\":\"00698\",\"dependencyId\":6808,\"enforceId\":6813,\"itemType\":1,\"latitude\":\"22.537009\",\"longitude\":\"113.955380\",\"name\":\"测试建筑00698\",\"personList\":[{\"certificateNo\":\"\",\"name\":\"测试\",\"personType\":1,\"phone\":\"13530695823\"}],\"rescueId\":6818,\"type\":1,\"underLevel\":1,\"upperLevel\":2,\"zoneCode\":\"001009001\"}";
        val newBuildDTO = NewBuildDTO()
        newBuildDTO.code = "0068"
        newBuildDTO.dependencyId = 6808L
        newBuildDTO.enforceId = 6813L
        newBuildDTO.itemType = 1
        newBuildDTO.latitude = "22.537009"
        newBuildDTO.longitude = "113.955380"
        newBuildDTO.name = "测试建筑00698"
        newBuildDTO.rescueId = 6818L
        newBuildDTO.type = 1
        newBuildDTO.underLevel = 2
        newBuildDTO.upperLevel = 2
        newBuildDTO.zoneCode = "001009001"
        val personList = ArrayList<BuildPersonnelDTO>()
        val buildPersonnelDTO = BuildPersonnelDTO()
        buildPersonnelDTO.name = "测试"
        buildPersonnelDTO.phone = "13530688956"
        buildPersonnelDTO.personType = 1
        personList.add(buildPersonnelDTO)
        newBuildDTO.personList = personList
        val requestParams = RequestParams.builder(HttpConstant.POST_ADD_BUILD, mHttpTag)
            .post().requestBodyObj(newBuildDTO).build()
        enqueue(requestParams, object : HttpCallBack<Long?> {
            override fun success(response: Long?) {
                LogUtils.i("新增成功：$response")
                showToast("新增成功:$response")
            }

            override fun fail(code: Int, msg: String?) {
                showToast(msg)
            }
        })
    }

    fun uploadFile(file: File, mHttpTag: String?, uploadListener: UploadListener?) {
        val filePath = file.absolutePath
        val requestParams = RequestParams.builder(HttpConstant.POST_UPLOAD_FILE, mHttpTag)
            .putFile("file", filePath).post().build()
        enqueue<ResponseBody<FileRes>>(
            requestParams,
            object : HttpCallBack<ResponseBody<FileRes>?> {
                override fun success(response: ResponseBody<FileRes>?) {
                    LogUtils.i(response.toString())
                    mFileUrl = response?.data?.src
                    showToast(mFileUrl)
                }

                override fun fail(code: Int, msg: String?) {
                    showToast(msg)
                }
            },
            uploadListener
        )
    }

    fun download(mHttpTag: String?, downloadListener: DownloadListener?) {
        if (TextUtils.isEmpty(mFileUrl)) {
            mFileUrl = "/file/210226//f7d554ce-8b26-4dd4-bd51-24e460c647d4.mp4"
        }
        if (isDownload(mFileUrl)) {
            LogUtils.i("文件已存在！")
        }
        var url = String.format("/api/v1%s", mFileUrl)
        //        url = "https://scpic.chinaz.net/files/pic/pic9/202103/apic31219.jpg";
        url = "https://www.gifbin.com/bin/052018/driver-mistakes-bikers.webm"
        val requestParams = RequestParams.builder(url, mHttpTag).build()
        download(
            requestParams,
            object : HttpCallBack<String?> {
                override fun success(s: String?) {
                    LogUtils.i("下载成功：$s")
                    showToast("下载成功：$s")
                }

                override fun fail(code: Int, msg: String?) {
                    showToast(msg)
                }
            },
            downloadListener
        )
    }

    private fun showToast(msg: String?) {
        Toast.makeText(Utils.getApp(), msg, Toast.LENGTH_SHORT).show()
    }
}